// ////////////////////////////////////////////////////////////////////////////////////////
// NAME: Blocker.java 
// AUTHOR: CDT Riley Mckenna 
// LAST MODIFIED: 28 March 2012 
// DESCRIPTION: Contains code for blocking and unblocking an application
// LIMITATIONS: 
// CITATIONS: In code
// 
// ////////////////////////////////////////////////////////////////////////////////////////
package prototype.revision;

import java.util.ArrayList;

import android.util.Log;

public class Blocker {
	private PermissionsManager pmgr;
	private FileOperations fo;
	private String installedPrograms = "installed_programs.txt";
	private String TAG = "block";

	public Blocker(FileOperations fileOp) {
		fo = fileOp;
		pmgr = new PermissionsManager();
	}

	public void block(String packageName) {
		pmgr.exec(true, packageName, pmgr.getINTERNET());
		Log.d(TAG, packageName + "");
		if (fo.SearchFile(packageName, fo.getInstalledPackages(), true) & !fo.SearchFile(packageName, fo.getBlackList(), true) & !fo.SearchFile(packageName, fo.getWhiteList(), true) ) {
			fo.AppendFile(packageName, fo.getBlackList());
		}else if (fo.SearchFile(packageName, fo.getWhiteList(), true) & !fo.SearchFile(packageName, fo.getBlackList(), true) ){
			fo.RemoveStrFromFile(fo.getWhiteList(), packageName);
			fo.AppendFile(packageName, fo.getBlackList());
		} else if (fo.SearchFile(packageName, fo.getWhiteList(), true) & fo.SearchFile(packageName, fo.getBlackList(), true) ){
			fo.RemoveStrFromFile(fo.getWhiteList(), packageName);
		}else {
			//TODO toast popper
			
		}
		
	}

	public void unBlock(String packageName) {
		pmgr.exec(false, packageName, pmgr.getINTERNET());
		if (fo.SearchFile(packageName, fo.getInstalledPackages(), true) & !fo.SearchFile(packageName, fo.getWhiteList(), true) & !fo.SearchFile(packageName, fo.getBlackList(), true)) {
			fo.AppendFile(packageName, fo.getWhiteList());
		}else if (fo.SearchFile(packageName, fo.getBlackList(), true) & !fo.SearchFile(packageName, fo.getWhiteList(), true)){
			fo.RemoveStrFromFile(fo.getBlackList(), packageName);
			fo.AppendFile(packageName, fo.getWhiteList());
		}else if (fo.SearchFile(packageName, fo.getBlackList(), true) & fo.SearchFile(packageName, fo.getWhiteList(), true)){
			fo.RemoveStrFromFile(fo.getBlackList(), packageName);
		}
		else {
			//TODO toast popper
		}
	}
	
	private void addToWhiteList(String packageName, String permission) {
		//pmgr.exec(false, packageName, pmgr.getINTERNET());
		if (fo.SearchFile(packageName, fo.getInstalledPackages(), true) & !fo.SearchFile(packageName, fo.getWhiteList(), true) & !fo.SearchFile(packageName, fo.getBlackList(), true)) {
			fo.AppendFile(packageName, fo.getWhiteList());
		}else if (fo.SearchFile(packageName, fo.getBlackList(), true) & !fo.SearchFile(packageName, fo.getWhiteList(), true)){
			fo.RemoveStrFromFile(fo.getBlackList(), packageName);
			fo.AppendFile(packageName, fo.getWhiteList());
		}else if (fo.SearchFile(packageName, fo.getBlackList(), true) & fo.SearchFile(packageName, fo.getWhiteList(), true)){
			fo.RemoveStrFromFile(fo.getBlackList(), packageName);
		}
		else {
			//TODO toast popper
		}
	}
	private void addToBlacklist(String packageName, String permission) {
		//pmgr.exec(true, packageName, pmgr.getINTERNET());
		Log.d(TAG, packageName + "");
		if (fo.SearchFile(packageName, fo.getInstalledPackages(), true) & !fo.SearchFile(packageName, fo.getBlackList(), true) & !fo.SearchFile(packageName, fo.getWhiteList(), true) ) {
			fo.AppendFile(packageName, fo.getBlackList());
		}else if (fo.SearchFile(packageName, fo.getWhiteList(), true) & !fo.SearchFile(packageName, fo.getBlackList(), true) ){
			fo.RemoveStrFromFile(fo.getWhiteList(), packageName);
			fo.AppendFile(packageName, fo.getBlackList());
		} else if (fo.SearchFile(packageName, fo.getWhiteList(), true) & fo.SearchFile(packageName, fo.getBlackList(), true) ){
			fo.RemoveStrFromFile(fo.getWhiteList(), packageName);
		}else {
			//TODO toast popper
			
		}
		
	}
	
	public void blockall(String permission){
		String[] installPrograms = fo.ReadFileAsList("installed_programs.txt");
		System.out.println(installPrograms + " list of installed programs (puffy)");
		for (int i = 0; i < installPrograms.length; i ++ ){
			String program = installPrograms[i];
			Log.d(program, ":program, i=" + i);
			this.addToBlacklist(program, permission);
		}
		
		
	}
	public void unblockall(String permission){
		String[] installPrograms =fo.ReadFileAsList("installed_programs.txt");
		System.out.println(installPrograms + " list of installed programs (puffy)");
		for (int i = 0; i < installPrograms.length; i ++ ){
			String program = installPrograms[i];
			Log.d(program, ":program, i=" + i);
			this.addToWhiteList(program, permission);
		}
		
		
	}

}